package com.yushu.base.controller;

import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.yushu.base.pojo.vo.DicVO;
import com.yushu.base.pojo.bo.Dic;
import com.yushu.base.service.DicService;
import com.yushu.common.execption.CgException;
import com.yushu.common.anno.LogAop;
import com.yushu.common.shiro.ShiroUtil;
import com.yushu.common.util.pager.Pages;
import com.yushu.common.util.result.Result;

import org.apache.commons.lang3.StringUtils;

//import io.swagger.annotations.Api;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


 /**
 * @author test
 * @CreateDate 2020-12-03 15:47:25
 */
//@Api(description = "字典管理")
@Controller
@RequestMapping("/base/dic")
public class DicController {
     private Logger logger = Logger.getLogger(DicController.class);

    @Autowired
    private DicService dicService;
    private Map<String, Object> resultMap;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   //true:允许输入空值，false:不能为空值
    }

    /**
     * 分页条件查询字典数据
     * 2020-03-08 11:32:46
     *
     * @param tablePage
     * @return
     */
    @LogAop("分页查询字典")
    @ResponseBody
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Pages page(@RequestBody Pages<DicVO> tablePage) {
        Pages page = dicService.page(tablePage);
        return page;
    }

    /**
     * 查询全部字典数据
     * 2020-03-08 11:32:46
     */
    @ResponseBody
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    public List<Dic> queryList(@RequestBody Dic dic) {
        List<Dic> list = dicService.selectList(dic);
        return list;
    }

    /**
     * 添加或者更新字典
     * 2020-03-08 11:32:46
     *
     * @param dic
     * @return
     */
    @LogAop("新增字典")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Result save(@RequestBody DicVO dic) {
        try {
            logger.info("进入添加字典方法");
            dicService.insert(dic);
            return Result.success("字典新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }


    @LogAop("编辑字典")
    @RequestMapping(value = "/editor", method = RequestMethod.POST)
    @ResponseBody
    public Result editor(@RequestBody DicVO dic) {
        try {
            logger.info("进入修改字典方法");
            dicService.update(dic);
            return Result.success("字典修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }


    /**
     * 批量删除字典数据
     * 2020-03-08 11:32:46
     *
     * @param dics 字典集合
     */
    @LogAop("批量删除字典")
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    @ResponseBody
    public Result batchDelete(Model model, @RequestBody List<DicVO> dics) {
        logger.info("进入到删除字典数据方法");
        try {
            dicService.deleteBatch(dics);
            return Result.success("批量删除字典成功");
        } catch (CgException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());

        }
    }

    /**
     * 删除字典数据
     * 2020-03-08 11:32:46
     *
     * @param dic 需要删除的对象
     */
    @LogAop("删除字典")
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result delete(Model model, @RequestBody Dic dic) {
        try {
            dicService.deleteById(dic.getId());
            logger.info("删除字典成功");
            return Result.success("删除字典成功");
        } catch (CgException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }


    /**
     * 跳转字典列表页面
     * 2020-03-08 11:32:46
     */
    @RequestMapping(value = "toDicList", method = RequestMethod.GET)
    public String toDicList(Model model) {
        model.addAttribute("title", "字典管理");
        model.addAttribute("page", "base/dic/list");
        return "menuPage";
    }

    /**
     * 跳转字典跳转到空白列表页面
     * 2020-03-08 11:32:46
     */
    @RequestMapping(value = "toOpenDicList", method = RequestMethod.GET)
    public String toOpenDicList(Model model, HttpServletRequest request) {
        model.addAttribute("title", "字典管理");
        model.addAttribute("page", "base/dic/list");
        return "openPage";
    }

    /**
     * 跳转字典新增或修改页面
     * 2020-03-08 11:32:46
     */
    @RequestMapping(value = "toEditor", method = RequestMethod.GET)
    public String toEditor(Model model, String id, HttpServletRequest request) {
        if (StringUtils.isNotEmpty(id)) {
            DicVO vo = dicService.selectDicVOById(id);
            request.setAttribute("data", vo);
        }
        model.addAttribute("title", "字典管理");
        model.addAttribute("page", "base/dic/addUpdate");
        return "openPage";
    }

    @RequestMapping("checkRepeat")
    @ResponseBody
    public String checkRepeat(@RequestBody Map map) {
        return dicService.checkRepeat(map);
    }
    
    //    校验字典名称是否名称重复
     @ResponseBody
     @RequestMapping(value = "/validateDName", method = RequestMethod.POST)
     public String validateDicName(String name,String key){
         try {
             return dicService.validateDName(name,key);
         }catch (Exception e){
             e.printStackTrace();
             return "false";
         }
     }
}

